package com.yjf.ljr.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.TextView;

public class SPreferences {

	public static final String SP_NAME = "sp_info";

	// 程序是否第一次使用
	private static final String FIRST_USE = "first_use";
	
	// 欢迎页图片启动时间
	private static final String KEY_WELCOME_IMG_TIME = "key_welcome_img_start_time";
	
	// 登录成功后用户的userId
	private static final String USER_ID = "userId";
	
	// 登录成功后用户的name
	private static final String USER_NAME = "userName";
	
	// 登录成功后用户的userType
	private static final String USER_TYPE = "userType";

	// 最后一次帮助中心数据修改的时间
	public static final String KEY_HELP_UPDATE_TIME = "key_help_update_time";

	// 帮助中心数据存储
	public static final String KEY_HELP_CENTER_DATA = "key_help_center_data";
	
	public static final String PASSWORD = "password";

	public static boolean isFirst(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.getInt(FIRST_USE, 0) == 0 ? true : false;
	}

	public static boolean setFirstRun(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.edit().putInt(FIRST_USE, 1).commit();
	}

	public static String getWelComeImgTime(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.getString(KEY_WELCOME_IMG_TIME, null);
	}

	public static boolean setWelComeImgTime(Context context, String time) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.edit().putString(KEY_WELCOME_IMG_TIME, time).commit();
	}

	public static String getUserId(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.getString(USER_ID, null);
	}

	public static boolean setUserId(Context context, String userid) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.edit().putString(USER_ID, userid).commit();
	}

	public static String getUserName(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.getString(USER_NAME, null);
	}

	public static boolean setUserName(Context context, String username) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.edit().putString(USER_NAME, username).commit();
	}

	public static String getUserType(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.getString(USER_TYPE, null);
	}

	public static boolean setUserType(Context context, String usertype) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return sp.edit().putString(USER_TYPE, usertype).commit();
	}
	
	
	public static boolean setPassword(Context context,String password) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String pwd = CyptoUtils.encode(password);
		if(TextUtils.isEmpty(pwd)) {
			sp.edit().remove(PASSWORD);
		}
		return sp.edit().putString(PASSWORD, pwd).commit();
	}
	
	public static String getPassword(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String pwd = sp.getString(PASSWORD, null);
		return CyptoUtils.decode(pwd);
	}

	public static void clearPassword(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		sp.edit().remove(PASSWORD);
	}

	//========================================================================================================
	/**
	 * 保存数据
	 * @param context
	 * @param data
	 * @param key
	 * @return
	 */
	public static boolean saveData(Context context, Object data, String key){
		return  saveData(context, data, key, SP_NAME);
	}

	/**
	 * 保存数据
	 * @param context
	 * @param data
	 * @param key
	 * @param spName
	 * @return
	 */
	public static boolean saveData(Context context, Object data, String key, String spName){
		boolean bResult = false;
		SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
		if (context != null) {
			if (data instanceof Integer) {
				bResult = sp.edit().putInt(key, (Integer)data).commit();
			} else if (data instanceof Long) {
				bResult = sp.edit().putLong(key, (Long)data).commit();
			} else if (data instanceof Float) {
				bResult = sp.edit().putFloat(key, (Float)data).commit();
			} else if (data instanceof Boolean) {
				bResult = sp.edit().putBoolean(key, (Boolean)data).commit();
			} else if (data instanceof String) {
				bResult = sp.edit().putString(key, (String)data).commit();
			}
		}
		return  bResult;
	}
	
	/**
	 * 获取数据
	 * @param context
	 * @param defaultValue
	 * @param key
	 * @return 数据类型需要强制转换
	 */
	public static Object getData(Context context, Object defaultValue, String key){
		return  getData(context, defaultValue, key, SP_NAME);
	}	
	/**
	 * 获取数据
	 * @param context
	 * @param defaultValue
	 * @param key
	 * @param spName
	 * @return 数据类型需要强制转换
	 */
	public static Object getData(Context context, Object defaultValue, String key, String spName){
		Object bResult = defaultValue;
		SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
		if (context != null) {
			if (defaultValue instanceof Integer) {
				bResult = sp.getInt(key, (Integer)defaultValue);
			} else if (defaultValue instanceof Long) {
				bResult = sp.getLong(key, (Long)defaultValue);
			} else if (defaultValue instanceof Float) {
				bResult = sp.getFloat(key, (Float)defaultValue);
			} else if (defaultValue instanceof Boolean) {
				bResult = sp.getBoolean(key, (Boolean)defaultValue);
			} else if (defaultValue instanceof String) {
				bResult = sp.getString(key, (String)defaultValue);
			}
		}
		return  bResult;
	}
	
}
